package com.nhimeye.admin.ui;

import java.util.HashSet;

import org.springframework.beans.factory.annotation.Autowired;

import com.nhimeye.admin.app.AbstractEntityView;
import com.nhimeye.admin.app.EntityEditor;
import com.nhimeye.admin.app.EntityProviderUtil;
import com.nhimeye.admin.app.H1;
import com.nhimeye.domain.ContentVersion;
import com.nhimeye.domain.Space;
import com.nhimeye.service.SpaceService;
import com.vaadin.addon.jpacontainer.EntityItem;
import com.vaadin.addon.jpacontainer.JPAContainer;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.spring.roo.addon.annotations.RooVaadinEntityView;
import com.vaadin.ui.Button;
import com.vaadin.ui.Table;


public class SpaceView extends AbstractEntityView<Space> {

	@Autowired
	SpaceService spaceService;
	
	private JPAContainer<Space> tableContainer;

    @Override
    protected void configureTable(Table table) {
        table.setContainerDataSource(getTableContainer());
        table.setVisibleColumns(getTableColumns());

    }

	@Override
	protected HashSet<String> getIgnoreTableColumns() {
		return new HashSet<String>();
	}

	@Override
	protected Class<? extends Space> getEntityClass() {
		 return Space.class;
	}

	@Override
	protected String getEntityName() {
		return "Space";
	}

	@Override
	public boolean isCreateAllowed() {
		return true;
	}

	@Override
	protected boolean isUpdateAllowed() {
		return true;
	}

	@Override
	protected boolean isDeleteAllowed() {
		return true;
	}

	@Override
	protected boolean isNewEntity(Space entity) {
		  return (entity != null && getIdForEntity(entity) == null);
	}

	@Override
	protected void deleteEntity(Space entity) {
		if (entity != null) {
			spaceService.deleteSpace(entity);
	        }
		
	}

	@Override
	protected Space saveEntity(Space entity) {
		if (entity == null) {
            return null;
        }
		spaceService.saveSpace(entity);
         return entity;
         
	}

	@Override
	public Object getIdProperty() {
		return "id";
	}

	@Override
	public Object getVersionProperty() {
		return "version";
	}

	@Override
	protected Container getTableContainer() {
		if (tableContainer == null) {
            JPAContainer<Space> container = new JPAContainer<Space>(Space.class);
            container.setEntityProvider(EntityProviderUtil.get().getEntityProvider(Space.class));
            tableContainer = container;
        }
        return tableContainer;
	}

	@Override
	protected Item getItemForEntity(Space entity) {
		Item item = getTable().getItem(entity.getId());
        if (item == null) {
            item = tableContainer.createEntityItem(entity);
        }
        return item;	}

	@Override
	protected Space getEntityForItem(Item item) {
		 if (item instanceof EntityItem && ((EntityItem) item).getEntity() instanceof Space) {
	            return (Space) ((EntityItem) item).getEntity();
	        } else {
	            return null;
	        }	}

	@Override
	public Object getIdForEntity(Space entity) {
		return entity != null ? entity.getId() : null;
	}

	@Override
	protected Space createEntityInstance() {
		 return new Space();
	}

	@Override
	protected void configureButton(Button b) {
		b.setCaption("+ Add Space");
		
	}

	@Override
	protected void configureCaption(H1 title) {
		title.setCaption("Spaces");
		
	}

	@Override
	protected EntityEditor createForm() {
		return new SpaceForm();
	}

}
